package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.capital.view.widget.NoSymbolputNewFilter;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityAddServiceNodeBinding;
import com.sgb.kjwl.model.entity.enterprise.ServiceNodeListEntity;
import com.sgb.kjwl.utils.EditTextUtils;
import com.sgb.kjwl.viewmodel.enterprise.AddServiceNodeViewModel;
import com.swgk.core.customview.InputFilterUtils;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class AddServiceNodeActivity extends AppActivity {

    private ActivityAddServiceNodeBinding mBinding;
    private AddServiceNodeViewModel mViewModel;


    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(AddServiceNodeViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_service_node);
        mBinding.setVariable(BR.mViewModel, mViewModel);
        mBinding.setLifecycleOwner(this);

        mViewModel.id = getIntent().getStringExtra("id");
        if (TextUtils.isEmpty(mViewModel.id)) {
            mViewModel.data= (ServiceNodeListEntity) getIntent().getSerializableExtra("data");
            mBinding.itvName.setContent( mViewModel.data.getNodeName());
            mBinding.tvContentData.setText( mViewModel.data.getNodeInstructions());
            mBinding.itvOrdernumber.setContent( mViewModel.data.getSort()+"");
        }
        mBinding.itvOrdernumber.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mBinding.itvOrdernumber.getContent_edt().setFilters(new InputFilter[]{new NoSymbolputNewFilter()});
        mBinding.tvContentData.setFilters(new InputFilter[]{new NoSymbolputNewFilter()});
        EditTextUtils.setEditTextFlag( mBinding.itvOrdernumber.getContent_edt(), null, 2);
        InputFilterUtils.setEditTextInhibitInputSpaChat(mBinding.tvContentData, 50);
        InputFilterUtils.setEditTextInhibitInputSpaChat(mBinding.itvName.getContent_edt(), 20);
        EditTextUtils.setEditTextFlag( mBinding.tvContentData, null, 50);
        mBinding.tvSubmit.setOnClickListener(this);
        initObserver();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_submit:
                if(!ClickUtils.isDoubleClick()) {
                    if (TextUtils.isEmpty(mBinding.itvName.getContent_edt().getText().toString())) {
                        mViewModel.toast.postValue("请输入节点名称");
                        return;
                    }
                    if (TextUtils.isEmpty(mBinding.tvContentData.getText().toString())) {
                        mViewModel.toast.postValue("请输入节点说明");
                        return;
                    }
                    if (TextUtils.isEmpty(mBinding.itvOrdernumber.getContent_edt().getText().toString())) {
                        mViewModel.toast.postValue("请输入排序");
                        return;
                    }
                    Map<String, Object> value = new HashMap<>();
                    if (!TextUtils.isEmpty(mViewModel.id)) {
                        value.put("serviceId", mViewModel.id);
                    } else {
                        value.put("serviceId", mViewModel.data.getServiceId());
                        value.put("id", mViewModel.data.getId());

                    }
                    value.put("nodeName", mBinding.itvName.getContent_edt().getText().toString().trim().replaceAll(" ", ""));
                    value.put("sort", mBinding.itvOrdernumber.getContent_edt().getText().toString().trim().replaceAll(" ", ""));
                    value.put("nodeInstructions", mBinding.tvContentData.getText().toString().trim().replaceAll(" ", ""));
                    mViewModel.submitdata(value);
                }
                break;
        }
    }

    private void initObserver(){
        mViewModel.ShowLoading.observe(this,type->{
            switch (type){
                case 1:
                    DialogHelper.showProgressDialog(this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;
                case 3:
                    DialogHelper.dismissProgressDialog();
                    finish();
                    break;
            }
        });

        mViewModel.toast.observe(this,String ->{
            MToast.showLongToast(this,String);
        });

    }

    public static void startactivity(Context mContext, String id, ServiceNodeListEntity data) {
        Intent mIntent = new Intent(mContext, AddServiceNodeActivity.class);
        if (!TextUtils.isEmpty(id)) {
            mIntent.putExtra("id", id);
        }
        if (data != null) {
            mIntent.putExtra("data", (Serializable) data);
        }
        mContext.startActivity(mIntent);
    }

}
